import logging
import unittest
import requests
import utils
from api.login import LoginApi
from api.trust_account import TrustAccountApi
from bs4 import BeautifulSoup


class TestWherherifLogin(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.login_api = LoginApi(self.session)
        self.trust_register = TrustAccountApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    # 开户成功
    def test01_trust_register(self):
        # 测试数据
        keywords = "13203003591"
        password = "hyc123456"

        # 调用接口
        res_login = self.login_api.login(keywords, password)
        res_trust_register = self.trust_register.trust_register()

        # 断言
        utils.common_assert(self, res_login, 200, 200, "登录成功")
        utils.common_assert(self, res_trust_register, 200, 200, None)

        # 输出日志
        logging.info("登录的结果为：{}".format(res_login.json()))
        logging.info("实名认证的结果为：{}".format(res_trust_register.json()))

        # 提取开户接口的响应数据
        html = res_trust_register.json()["description"].get("form")

        # 调用第三方函数提取数据
        action, form_data = utils.parse_html(html)

        # 输出日志
        logging.info("提取的结果为：{}".format(form_data))

        # 调用第三方开户接口
        res_trust_register_third = self.session.post(action, data=form_data)
        # 断言
        self.assertEqual(200,res_trust_register_third.status_code)
        # 输出日志
        logging.info("第三方开户接口返回的结果为：{}".format(res_trust_register_third.text))
